package com.tajiang.business.businesscount.wallet;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.tajiang.business.R;
import com.tajiang.business.businesscount.adapter.TradeRecordAdapter;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.utils.BottomCallBackInterface;
import com.tajiang.business.common.utils.PostDataTools;
import com.tajiang.business.model.AccountWithDrawInfo;
import com.tajiang.business.model.Order;
import com.tajiang.business.model.Pager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TradeRecordActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_to_load_layout)
    SwipeToLoadLayout swipeToLoadLayout;

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;

    private TradeRecordAdapter mAdapter;
    private List<AccountWithDrawInfo> dataList = new ArrayList<AccountWithDrawInfo>();

    private int pageNumber = 1;//当前页

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTopBar() {
        setTitle("交易记录");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_trade_record);
    }

    @Override
    protected void initData() {

        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setLoadMoreEnabled(false);

        mAdapter = new TradeRecordAdapter(TradeRecordActivity.this, dataList, new BottomCallBackInterface.OnBottomClickListener() {

            @Override
            public void onBottomClick(View v, int position) {

            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        onRefresh();
    }

    @Override
    public void onRefresh() {

        pageNumber = 1;
        PostDataTools.acct_tradlist(this, pageNumber, swipeToLoadLayout, new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                Pager<AccountWithDrawInfo> pager = (Pager) object;
                List<AccountWithDrawInfo> newList = pager.getList();

                dataList.clear();
                dataList.addAll(newList);
                mAdapter.notifyDataSetChanged();

                if (newList.size() >= Order.PAGE_SIZE){
                    pageNumber = 2;
                    swipeToLoadLayout.setLoadMoreEnabled(true);
                }else{
                    swipeToLoadLayout.setLoadMoreEnabled(false);
                }
            }
        });
    }

    @Override
    public void onLoadMore() {

        PostDataTools.acct_tradlist(this, pageNumber, swipeToLoadLayout, new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                Pager pager = (Pager) object;
                List<AccountWithDrawInfo> newList = pager.getList();

                dataList.addAll(newList);
                mAdapter.notifyDataSetChanged();

                if (newList.size() >= Order.PAGE_SIZE){
                    ++pageNumber;
                    swipeToLoadLayout.setLoadMoreEnabled(true);
                }else{
                    swipeToLoadLayout.setLoadMoreEnabled(false);
                }
            }
        });
    }

}
